﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEditor;
using UnityEditor.U2D;
using Object = System.Object;

public class UIBehaviourEditorData : ScriptableObject
{
    public bool AutoBind;
    public bool IsBindObject;
    public GameObject BindObject;
}

[CustomEditor(typeof(UIBehaviour))]
public class UIBehaviourEditor : Editor
{
    private SerializedProperty Intercept;
    private SerializedProperty AutoBind;
    private SerializedProperty HandleClass;
    private SerializedProperty EventHandlers;
    private SerializedProperty HasHotFixObject;
    private SerializedProperty HotFixObjectTag;

    private UIBehaviour _behaviour;
    private bool needReset = false;
    private string oldHotFixCls;

    //tips
    private GameObject BindParent;
    void OnEnable()
    {
        AutoBind = serializedObject.FindProperty("AutoBind");
        Intercept = serializedObject.FindProperty("Intercept");
        HandleClass = serializedObject.FindProperty("HandleClsName");
        EventHandlers = serializedObject.FindProperty("EventHandlers");

        HasHotFixObject = serializedObject.FindProperty("HasHotFixObject");
        HotFixObjectTag = serializedObject.FindProperty("HotFixObjectTag");

        _behaviour = (UIBehaviour)target;
    }

    public override void OnInspectorGUI()
    {
        serializedObject.Update();
        EditorGUILayout.PropertyField(HasHotFixObject);
        EditorGUI.BeginDisabledGroup(HasHotFixObject.boolValue);
        EditorGUILayout.PropertyField(HotFixObjectTag);
        EditorGUI.EndDisabledGroup();
        EditorGUILayout.Space();

        EditorGUILayout.PropertyField(AutoBind);
        ValueChanged();
        serializedObject.ApplyModifiedProperties();
        AutoGenHandlers();
    }

    private void AutoGenHandlers()
    {
        if (GUILayout.Button("GenerateHandlers"))
        {
            string baseName = GetBaseName();
            foreach (var handler in _behaviour.EventHandlers)
            {
                if (handler.Function == null || handler.Function.Trim() == "")
                {
                    handler.Function = GenerateActionHandlerName(baseName, handler.ActionType);
                }
            }
        }

        if (GUILayout.Button("ReGenerateAllHandlers"))
        {
            string baseName = GetBaseName();
            foreach (var handler in _behaviour.EventHandlers)
            {
                handler.Function = GenerateActionHandlerName(baseName, handler.ActionType);
            }
        }
        serializedObject.Update();
        EditorGUILayout.PropertyField(EventHandlers, true);
        serializedObject.ApplyModifiedProperties();
    }

    private string GetBaseName()
    {
        var splits = _behaviour.gameObject.name.Split(new char[3] { ' ', '-', '_' });
        string baseName = "on";
        foreach (var word in splits)
        {
            Debug.Log(word);
            if (word != "")
            {
                baseName = baseName + word.Substring(0, 1).ToUpper() + word.Substring(1);
            }
        }

        return baseName;
    }

    private string GenerateActionHandlerName(string baseName, UIBehaviour.UIActionType type)
    {
        switch (type)
        {
            case UIBehaviour.UIActionType.CLICK:
                return baseName + "Click";
            case UIBehaviour.UIActionType.DOUBLE_CLICK:
                return baseName + "DBClick";
            case UIBehaviour.UIActionType.SUBMIT:
                return baseName + "Summit";
            case UIBehaviour.UIActionType.HOVER:
                return baseName + "Hover";
            case UIBehaviour.UIActionType.PRESS:
                return baseName + "Press";
            case UIBehaviour.UIActionType.SELECT:
                return baseName + "Select";
            case UIBehaviour.UIActionType.SCROLL:
                return baseName + "Scroll";
            case UIBehaviour.UIActionType.DRAG_START:
                return baseName + "DragStart";
            case UIBehaviour.UIActionType.DRAG:
                return baseName + "Drag";
            case UIBehaviour.UIActionType.DRAG_OVER:
                return baseName + "DragOver";
            case UIBehaviour.UIActionType.DRAG_OUT:
                return baseName + "DragOut";
            case UIBehaviour.UIActionType.DRAG_END:
                return baseName + "DragEnd";
            case UIBehaviour.UIActionType.DROP:
                return baseName + "Drop";
            case UIBehaviour.UIActionType.KEY:
                return baseName + "Key";
            case UIBehaviour.UIActionType.TOOLTIP:
                return baseName + "Tooltip";
        }
        throw new Exception("UIBehaviour's Handler acition type error");
    }

    private void ValueChanged()
    {
        EditorGUI.BeginDisabledGroup(AutoBind.boolValue);
        //modify HotFixCls 值
        EditorGUILayout.PropertyField(Intercept);
        EditorGUILayout.PropertyField(HandleClass);
        EditorGUI.BeginDisabledGroup(true);
        BindParent = EditorGUILayout.ObjectField("BindObject", BindParent, typeof(GameObject), true) as GameObject;
        EditorGUI.EndDisabledGroup();

        if (AutoBind.boolValue)
        {
            UpdateHotFixCls(_behaviour.gameObject);
            needReset = true;
        }
        else
        {
            BindParent = null;
            if (needReset)
            {
                HandleClass.stringValue = oldHotFixCls;
                needReset = false;
            }
            else
            {
                oldHotFixCls = HandleClass.stringValue;
            }
        }
        EditorGUI.EndDisabledGroup();
    }

    private void UpdateHotFixCls(GameObject go)
    {
        var parent = go.transform.parent;
        while (parent != null)
        {
            var behaviour = parent.GetComponent<UIBehaviour>();
            if (behaviour != null && !behaviour.AutoBind && behaviour.Intercept)
            {
                HandleClass.stringValue = behaviour.HandleClsName;
                BindParent = parent.gameObject;
                return;
            }
            parent = parent.transform.parent;
        }

        if (parent == null)
        {
            HandleClass.stringValue = "UIHandler";
            BindParent = null;
        }
    }
}
